/*
 * File: Hailstone.java
 * Name: Diego A. Falciola
 * Section Leader: Sam King 
 * --------------------
 * This file is the starter file for the Hailstone problem.
 */

import acm.program.*;

public class Hailstone extends ConsoleProgram {
	
	/**
	 * Prompt the user for a number 
	 * and execute Hailstone sequence
	 */
	public void run() {
		executeHailstoneSequence(readPositiveInt());
	}
	
	/**
	 * Reads a positive integer from the user and returns it.
	 * 
	 * @return A positive integer entered by the user.
	 */
	private int readPositiveInt() {
		/* Get an initial value. */
		int value = readInt("Enter a positive integer: ");
		
		/* If the value was nonpositive, reprompt the user. */
		while (value <= 0) {
			println("Please enter a positive integer.");
			value = readInt("Enter a positive integer: ");
		}
		
		return value;
	}
	
	/**
	 *  Executes hailstone sequence until number 1 is reached
	 *  and prints number of iterations that the process took
	 * @param number initial hailstone sequence number
	 */
	private void executeHailstoneSequence(int number){
		int newNum = number;
		// count the number of iterations
		int iterations = 0;
		
 		while (newNum!=1){
 			// even number
			if (newNum % 2 == 0){
				newNum /= 2;
				println(number + "\t is even, so I take half:\t " + newNum);			
			}
			// odd number
			else{ 
				newNum = 3*newNum+1;
				println(number + "\t is odd, so I make 3n+1:\t " + newNum);
			}
			number = newNum;
			iterations ++;
		}
				
		println("The process took " + iterations + " to reach 1");
	}
}